<template>
  <!-- 修改盘点方案 -->
  <div class="stock">
    <div class="dropdown">
      <jc-button
        :btn-arr="['reload', 'pass', 'sub', 'option', 'remove', 'save', 'push']"
        :disab-arr="['toExcel', 'copy', 'using', 'cancel', 'com1', 'com2', 'freeze']"
        :fbillno="prodValue.fbillno"
        @save="subMarker"
        @submit="submit(1)"
        @noSubmit="submit(2)"
        @audit="audit(1)"
        @noAudit="audit(2)"
        @forbidden="using(1)"
        @startUsing="using(2)"
        @del="delet"
        @refresh="queryPlanDetail"
        @pushDown="isdanjuDialog=true"
      />
    </div>
    <el-tabs v-model="activeName" @tab-click="handleOther">
      <el-tab-pane :label="$t('market.info')" name="first">
        <el-form ref="purchaseRef" :model="prodValue" label-width="100px" :disabled="isDisabled" size="mini">
          <el-form-item :label="$t('market.state')">
            <el-tag :type="prodValue.stateColor">{{ prodValue.fdocumentstatus }}</el-tag>
          </el-form-item>
          <el-form-item :label="$t('market.billType')">
            <el-input v-model="prodValue.fbilltypeno" size="mini" disabled />
          </el-form-item>
          <el-form-item :label="$t('market.inventPlanName')">
            <el-input v-model="prodValue.fname" size="mini" />
          </el-form-item>
          <el-form-item :label="$t('market.inventPlanfbi')">
            <el-input v-model="prodValue.fbillno" size="mini" disabled />
          </el-form-item>
          <el-form-item :label="$t('market.note')">
            <el-input v-model="prodValue.fnote" size="mini" />
          </el-form-item>
          <el-form-item :label="$t('market.inventPlanper')">
            <el-radio v-model="prodValue.forthwithRep" label="1" :disabled="isDisabled">{{ $t('market.forthwithRep') }}</el-radio>
            <el-radio v-model="prodValue.forthwithRep" label="2" :disabled="isDisabled">{{ $t('market.lingCheck') }}</el-radio>
            <!--            <el-checkbox v-model="prodValue.fzerostockincount" :disabled="isDisabled">{{ $t('market.lingCheck') }}</el-checkbox>-->
          </el-form-item>
          <el-form-item :label="$t('market.inventdefau')">
            <el-input-number v-model="prodValue.fcheckqtydefault" size="mini" controls-position="right" />
          </el-form-item>
          <el-form-item :label="$t('market.stockNumber')">
            <el-input v-model="prodValue.fstockname" size="mini">
              <i
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="seleceVisiblit=true"
              />
            </el-input>
          </el-form-item>
          <el-form-item :label="$t('market.fmaterial')">
            <el-input v-model="prodValue.fnumber" size="mini">
              <i
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="isMateria=true"
              />
            </el-input>
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane :label="$t('market.other')" name="log">
        <jc-marker
          ref="log"
          :fnumber="prodValue.fbillno"
          other-height="65vh"
          :other-url-object="otherUrlObject"
        />
      </el-tab-pane>
    </el-tabs>
    <!--物料弹窗-->
    <material v-if="isMateria" @material="materialData" />
    <!--仓库-->
    <selectCK
      v-if="seleceVisiblit"
      :msg="1"
      @client="selectData"
    />
    <!--下推-->
    <el-dialog
      v-dialogDrag
      :title="$t('market.checkBill')"
      model
      :visible.sync="isdanjuDialog"
      :close-on-click-modal="false"
      width="600px"
      center
      append-to-body
    >
      <div style="text-align: center;height: 100px;line-height: 100px">
        <el-radio-group v-model="orderLX">
          <el-radio v-for="item in receipts" :key="item" :label="item">{{ item }}</el-radio>
        </el-radio-group>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="isdanjuDialog = false">{{ $t('btn.cancel') }}</el-button>
        <el-button size="mini" type="primary" @click="pushDownOrder()">{{ $t('btn.confirm') }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  deleteStkcountscheme,
  getStkcountschemeById, putStkInPut,
  stkCountAudit,
  stkCountAuSubmit,
  stkCountClose,
  stkCountSubmit,
  stkCountUnAudit,
  stkCountUnClose,
  updateStkcountscheme
} from '@/api/repertory/stock'
import autocomplete from '@/views/market/marketManage/createMarkerOrder/components/autocomplete'
import { notoMxBmina, toMxBmina } from '@/components/ToMxamineState'
import jcMarker from '@/components/marker'
import jcButton from '@/components/Button'

export default {
  components: {
    jcButton,
    jcMarker,
    selectCK: () => import('@/views/production/collect/components/seleceCK'),
    material: () => import('@/views/market/marketManage/createMarkerOrder/components/material')
  },
  mixins: [autocomplete],
  data() {
    return {
      isdanjuDialog: false,
      orderLX: this.$t('menu.operation'),
      receipts: [this.$t('menu.operation')],
      isMateria: false,
      isDisabled: false,
      seleceVisiblit: false,
      activeName: 'first',
      otherUrlObject: {},
      prodValue: {},
      fstockArr: [],
      fnumberArr: []
    }
  },
  created() {
    if (this.$route.params.id) {
      this.queryPlanDetail()
    }
  },
  methods: {
    // 详情
    queryPlanDetail() {
      const DATA = {
        fid: this.$route.params.id + ''
      }
      getStkcountschemeById(DATA).then(res => {
        if (res.code === 0) {
          this.prodValue = toMxBmina(res.data)
          this.fstockArr = this.prodValue.fstockidStr
          this.fnumberArr = this.prodValue.fmaterialidStr
          this.isDisabled = this.prodValue.fdocumentstatus === this.$t('market.state1') || this.prodValue.fdocumentstatus === this.$t('market.state2')
          if (this.prodValue.fzerostockincount === '1') {
            this.prodValue.fzerostockincount = true
          } else {
            this.prodValue.fzerostockincount = false
          }
          this.otherUrlObject = res.data
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            type: 'warning'
          })
        }
      })
    },
    // 保存
    subMarker() {
      const DATA = {
        ...notoMxBmina(this.prodValue),
        fzerostockincount: this.prodValue.fzerostockincount ? '1' : '0'
      }
      updateStkcountscheme(DATA).then(res => {
        if (res.code === 0) {
          this.queryPlanDetail()
          this.$message({ showClose: true, message: res.message, type: 'success' })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            type: 'warning'
          })
        }
      })
    },
    // 获取物料
    materialData(ev) {
      if (ev.fmaterialId) {
        this.fnumberArr.push(ev.fmaterialId)
        this.prodValue.fnumber += ev.fnumber + ','
        this.prodValue.fmaterialidStr = this.fnumberArr
      }
      this.isMateria = false
    },
    // 选择仓库
    selectData(ev) {
      if (ev.fstockId) {
        this.fstockArr.push(ev.fstockId)
        this.prodValue.fstockidStr = this.fstockArr
        this.prodValue.fstockname += ev.fname + ','
      }
      this.seleceVisiblit = false
    },
    handleOther(tab) {
      if (tab.name === 'log') {
        this.$refs.log.otherlog()
      }
    },
    // 公共函数
    toExamineFun(api, ev) {
      const DATA = { Numbers: [this.prodValue.fbillno] }
      api(DATA).then(res => {
        if (res.code !== 0) {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        if (ev === 1) {
          // 关闭当前页面
          this.$store.dispatch('tagsView/guanbi', this.$route)
          this.$router.push({ path: '/inventoryPlanList' })
          return
        }
        this.queryPlanDetail()
        this.selectVal = []
        this.innerHTM = ''
        res.data.forEach(res => {
          this.innerHTM += '<div>' + res + '</div>'
        })
        this.$notify.warning({
          title: '提示',
          message: this.innerHTM,
          duration: 3000, offset: 50,
          dangerouslyUseHTMLString: true
        })
      })
    },
    // 提交
    async submit(ev) {
      const api = ev === 1 ? stkCountSubmit : stkCountAuSubmit
      this.toExamineFun(api)
    },
    // 审核
    async audit(ev) {
      const api = ev === 1 ? stkCountAudit : stkCountUnAudit
      this.toExamineFun(api)
    },
    // 关闭
    async using(ev) {
      const api = ev === 1 ? stkCountClose : stkCountUnClose
      this.toExamineFun(api)
    },
    // 删除
    delet() {
      this.$confirm('确认要删除么？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        const api = deleteStkcountscheme
        this.toExamineFun(api, 1)
      })
    },
    // 下推
    pushDownOrder() {
      const DATA = {
        SoucrceIds: [this.prodValue.fid]
      }
      putStkInPut(DATA).then(res => {
        if (res.code === 0) {
          this.isdanjuDialog = false
          this.$message({ showClose: true, message: res.message, type: 'success' })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.stock {
  padding: 10px;
  background: #fff;
  min-height: calc(100vh - 100px);
  .el-input::v-deep .el-input__inner {
    height: 30px;
    line-height: 30px;
  }
  .el-form {
    display: flex;
    flex-direction: column;
    flex-wrap: wrap;

    .el-form-item {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      height: 35px;
      width: 350px;
      margin-bottom: 5px;
      .btnType {
        margin-right: 20px;
      }

      .el-date-editor.el-input {
        width: 100% !important;
      }

      .el-select {
        width: 100%;
      }
    }
  }
}
</style>
